import { TrainerDao } from '../dao/trainerDao';
import { Trainer } from '../../types/trainer';
import { Page, PageReq } from '../../types/page';
import { ExcelParser } from '../../utils/excel';
import { TrainerFileDao } from '../dao/trainerFileDao';
import { invoke } from '@tauri-apps/api/core';

export class TrainerService {
  // 创建患者
  static async create(trainer: Omit<Trainer, 'id'>) {
   // console.log(trainer);
    const trainer_id = await TrainerDao.create(trainer);
    if(trainer.trainer_files){
      for(const trainer_file of trainer.trainer_files){
        trainer_file.trainer_id = trainer_id;
        await TrainerFileDao.create(trainer_file);
      }
    }
  }

  // 更新患者信息
  static async update(trainer: Partial<Trainer>) {
    console.log(trainer);
     TrainerDao.update(trainer);
     if(trainer.trainer_files){
      TrainerFileDao.deleteByTrainerId(trainer.id);
      for(const trainer_file of trainer.trainer_files){
        trainer_file.trainer_id = trainer.id;
        await TrainerFileDao.create(trainer_file);
      }
    }
  }

  // 删除患者
  static async delete(id: number) {
    return TrainerDao.delete(id);
  }

  // 根据ID查询患者
  static async findById(id: number): Promise<Trainer | null> {
    const trainer = await TrainerDao.findById(id);
    if(trainer){
      trainer.trainer_files = await TrainerFileDao.findByTrainerId(id);
    }
    return trainer;
  }

  // 查询所有患者
  static async list(): Promise<Trainer[]> {
    const trainers = await TrainerDao.list();
    for(const trainer of trainers){
      trainer.trainer_files = await TrainerFileDao.findByTrainerId(trainer.id);
    }
    return trainers;
  }


    /**
     * 分页查询医生列表
     */
    static async page(params: PageReq<Trainer>): Promise<Page<Trainer>> {
      const page = await TrainerDao.page(params);
      for(const trainer of page.list){
        trainer.trainer_files = await TrainerFileDao.findByTrainerId(trainer.id);
      }
      //console.log(page);
      return page;
    }

    /**
     * 从Excel导入患者数据
     * @param file Excel文件
     */
    static async importFromExcel(file: File): Promise<void> {
      const trainers = await ExcelParser.parseTrainerExcel(file);
      
      // 使用事务批量导入
      await TrainerDao.execute('BEGIN TRANSACTION');
      try {
        for (const trainer of trainers) {
          await TrainerDao.create(trainer);
        }
        await TrainerDao.execute('COMMIT');
      } catch (error) {
        await TrainerDao.execute('ROLLBACK');
        throw error;
      }
    }

    /**
     * 下载患者导入模板
     * @returns Blob对象
     */
    static async downloadTemplate(): Promise<Blob> {
      return await ExcelParser.generateTemplate();
    }

    /**
     * 下载患者导入模板到用户选择的位置
     * @returns 保存的文件路径
     */
    static async downloadTemplateWithDialog(): Promise<string> {
      const blob = await ExcelParser.generateTemplate();
      const arrayBuffer = await blob.arrayBuffer();
      const fileData = new Uint8Array(arrayBuffer);
      
      return await invoke<string>('save_template_file', {
        fileData: Array.from(fileData)
      });
    }
}